<template>
  <div class="custom-manage">
    <el-card>
      <h3>
        日志管理
        <!-- <el-button type="primary" icon="el-icon-search" size="small" circle @click="search"></el-button> -->
        <el-input v-model="searchLogNumber" placeholder="操作者" style="width: 160px; margin-right: 10px;float: right;margin: 0px 90px 10px 10px;"></el-input>
        <el-button type="primary" size="small" circle @click="open" style="float: right;margin-top: 5px;">数据导出</el-button>
      </h3>
      <el-table style="width: 100%" :data="filteredLogs" border
        :cell-style="cellStyle">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column label="序号" type="index" :index="indexMethod" width="50"></el-table-column>
        <!-- <el-table-column prop="imgUrl" label="用户头像" width="120">
          <template slot-scope="scope">
            <img :src="scope.row.imgUrl" alt="" style="width: 100%;height: 50px;">
          </template>
</el-table-column> -->
        <el-table-column prop="logUser" label="操作者"> </el-table-column>
        <el-table-column prop="logLevel" label="操作等级"> </el-table-column>
        <el-table-column prop="logContent" label="操作内容"> </el-table-column>
        <el-table-column prop="createTime" label="开始日期"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-edit" circle
              @click="editUser(scope.row.logId)"></el-button>
            <el-button type="danger" size="small" circle icon="el-icon-delete"
              @click="deleteUser(scope.row.logId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
          :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-card>


    <el-dialog title="请填写会员类型" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="操作id" :label-width="formLabelWidth" hidden>
          <el-input v-model="form.logId" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item label="操作等级" :label-width="formLabelWidth">
          <!-- <el-input v-model="form.logLevel" autocomplete="off" placeholder="请输入操作等级"
            value="this.form.logLevel"></el-input> -->
          <el-select v-model="form.logLevel" placeholder="选择操作等级">
            <el-option label="INFO" value="INFO"></el-option>
            <el-option label="DEBUG" value="DEBUG"></el-option>
            <el-option label="WARN" value="WARN"></el-option>
            <el-option label="ERROR" value="ERROR"></el-option>
          </el-select>
          <!-- <el-radio v-model="form.logLevel" label="INFO">INFO</el-radio>
          <el-radio v-model="form.logLevel" label="DEBUG">DEBUG</el-radio>
          <el-radio v-model="form.logLevel" label="WARN">WARN</el-radio>
          <el-radio v-model="form.logLevel" label="ERROR">ERROR</el-radio> -->
        </el-form-item>
        <el-form-item label="操作者" :label-width="formLabelWidth">
          <el-input v-model="form.logUser" autocomplete="off" placeholder="请输入操作者" value="this.form.logUser"></el-input>
        </el-form-item>
        <el-form-item label="操作内容" :label-width="formLabelWidth">
          <el-input v-model="form.logContent" autocomplete="off" placeholder="请输入操作内容"
            value="this.form.logContent"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateType">更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getLogsPage, getLogsAll, logsExportIds, exportLogsExcel, deleteLog, updateLogs } from '@/api/log'

export default {
  name: 'MemberManage',
  data() {
    return {
      logList: [],
      searchLogNumber: '',
      currentPage: 1,
      total: 0,
      pageSize: 8,
      pageSizes: [],
      dialogFormVisible: false,
      form: {
        logId: '',
        logLevel: '',
        logUser: '',
        logContent: '',
        createTime: ''
      },
      formLabelWidth: '120px',
    }
  },

  async created() {
    for (let i = 0; i < 5; i++) {
      this.pageSizes.push(8 * (i + 1))
    }
    this.fetchData()
  },

  // watch: {
  //   dialogFormVisible(val) {
  //     if (val === false) {
  //       this.status = 0
  //     }
  //   }
  // },
  computed: {
    filteredLogs() {
      return this.logList.filter(log => {
        return !this.searchLogNumber || log.logUser.includes(this.searchLogNumber)
      }).slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
    },
  },
  methods: {
    fetchData() {
      getLogsAll().then((res) => {
        this.total = res.length
        this.logList = res
        const options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' }
        for (let i = 0; i < this.logList.length; i++) {
          if (!this.logList[i].logUser) {
            this.logList[i].logUser = "用户" + Math.floor(Math.random() * (10 - 0 + 1)) + 1;
          }
          this.logList[i].createTime = new Date(this.logList[i].createTime).toLocaleDateString(undefined, options)
        }
      })
    },
    deleteUser(id) {
      this.$confirm('此操作将永久删除该日志, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.warn(id)
        deleteLog(id).then(res => {
          this.fetchData()
          this.currentPage = 1
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editUser(id) {
      this.logList.forEach(item => {
        if (item.logId === id) {
          this.form = item
        }
      })
      console.warn(this.form)
      // this.status = id
      this.dialogFormVisible = true
    },
    updateType() {
      this.form.createTime = ''
      let logs = this.form
      updateLogs(logs).then(res => {
        this.$message({
          type: 'success',
          message: '更新成功!'
        })
        this.status = 0
        this.dialogFormVisible = false
        this.fetchData()
      }).catch(err => {
        this.$message({
          showClose: true,
          message: '更新失败' + err.message,
          type: 'error'
        });
      })
    },
    // search() {
    //   this.$prompt('请输入电话号搜索', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //   }).then(({ value }) => {
    //     console.warn("value")
    //     console.warn(value)
    //     for (let i = 0; i < this.logList.length; i++) {
    //       if (this.logList[i].telephone == value) {
    //         this.$message({
    //           type: 'success',
    //           message: '搜索到相关日志'
    //         });
    //         this.logList = [this.logList[i]]
    //         return
    //       }
    //     }
    //     this.$message({
    //       type: 'error',
    //       message: '没有搜索到相关日志'
    //     })
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '取消输入'
    //     });
    //   });
    // },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'logLevel') {
        switch (row.logLevel) {
          case 'INFO':
            return { color: '#6b798e', fontWeight: 'bold' }
          case 'DEBUG':
            return { color: '#5aa4ae', fontWeight: 'bold' }
          case 'WARN':
            return { color: '#ecd452', fontWeight: 'bold' }
          case 'ERROR':
            return { color: '#c12c1f', fontWeight: 'bold' }
          default:
            return {};
        }
      }
      return {};
    },
    //切换每页条数容量
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.fetchData()
    },
    //切换当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    logsExportIds,
    open() {
      window.open(process.env.VUE_APP_BASE_URL + '/excel/logs/export')
    },
    indexMethod(index) {
      return index + 1 + (this.currentPage - 1) * this.pageSize;
    },
  }
}
</script>

<style lang="less" scoped>
.custom-manage {
  display: flex;
  justify-content: center;
}

.el-card {
  display: flex;
  flex-direction: column;
  width: 94%;
  margin: 20px 0;

  h3 {
    position: relative;
    height: 35px;

    .el-button {
      position: absolute;
      right: 10px;
      top: 5;
      border-radius: 10px;
    }
  }

  .el-table {
    width: 100%;
    text-align: center;

    .el-table-column {
      text-align: center;
    }
  }
}

.el-pagination {
  margin: 10px 0 10px 300px;
}
</style>
